﻿using _12306helper.Service;
using _12306helper.UI.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace _12306helper.UI
{
    public partial class TrainQueryForm : Form
    {
        public TrainQueryForm()
        {
            InitializeComponent();
        }
        private void TrainQueryForm_Load(object sender, EventArgs e)
        {
            InitForm();
        }
        private void InitForm()
        {
            DateTime dt = DateTime.Now.AddDays(Double.Parse(Resources.saleDays));
            dtpDate.Value = dt;
            dtpDate.MaxDate = dt;
            dtpDate.MinDate = DateTime.Now;

            lbxFrom.Visible = false;
            lbxTo.Visible = false;
        }

        private void txtFrom_TextChanged(object sender, EventArgs e)
        {
            string from = txtFrom.Text;
            if (!string.IsNullOrEmpty(from))
            {
                lbxFrom.Visible = true;
                lbxFrom.DataSource = TrainService.StationAutoComplete(from);
            }
            else
            {
                lbxFrom.Visible = false;
            }
        }

        private void txtFrom_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                if (lbxFrom.SelectedIndex > 0) lbxFrom.SelectedIndex -= 1;
            }
            else if (e.KeyCode == Keys.Down)
            {
                if (lbxFrom.SelectedIndex < lbxFrom.Items.Count - 1) lbxFrom.SelectedIndex += 1;
            }
            else if (e.KeyCode == Keys.Enter)
            {
                txtFrom.Text = lbxFrom.SelectedItem.ToString();
                lbxFrom.Visible = false;
            }
        }

        private void txtTo_TextChanged(object sender, EventArgs e)
        {
            string to = txtTo.Text;
            if (!string.IsNullOrEmpty(to))
            {
                lbxTo.Visible = true;
                lbxTo.DataSource = TrainService.StationAutoComplete(to);
            }
            else
            {
                lbxTo.Visible = false;
            }
        }

        private void txtTo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)
            {
                if (lbxTo.SelectedIndex > 0) lbxTo.SelectedIndex -= 1;
            }
            else if (e.KeyCode == Keys.Down)
            {
                if (lbxTo.SelectedIndex < lbxTo.Items.Count - 1) lbxTo.SelectedIndex += 1;
            }
            else if (e.KeyCode == Keys.Enter)
            {
                txtTo.Text = lbxTo.SelectedItem.ToString();
                lbxTo.Visible = false;
            }
        }

        private void txtFrom_Leave(object sender, EventArgs e)
        {
            lbxFrom.Visible = false;
        }

        private void txtTo_Leave(object sender, EventArgs e)
        {
            lbxTo.Visible = false;
        }

    }
}
